package com.eiletxie.springcloud.controller;

import com.eiletxie.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author EiletXie
 * @Since 2020/3/11 21:05
 */
@RestController
@Slf4j
public class OrderHystrixController extends BaseController {
    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
  /*  @HystrixCommand(fallbackMethod = "paymentInfo_TimeoutFallbackMethod",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000")
    })
    @HystrixCommand*/
    public String paymentInfo_Timeout(@PathVariable("id") Integer id) {
        int i = 1 / 0;
        String result = paymentHystrixService.paymentInfo_Timeout(id);
        return result;
    }


    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeoutFallbackMethod", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000"),
         /*   @HystrixProperty(name = "execution.isolation.strategy" , value = "SEMAPHORE"),//信号量隔离
            @HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests" , value = "2") //信号量大小*/
            @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),  //使用线程池的隔离
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "9000")  //超时设置为3秒

    },
            threadPoolProperties = {

                    @HystrixProperty(name = "coreSize", value = "1"), //线程池大小
                    @HystrixProperty(name = "maxQueueSize", value = "1"), //等待队列长度
                    @HystrixProperty(name = "keepAliveTimeMinutes", value = "2"),//线程存活时间
                    @HystrixProperty(name = "queueSizeRejectionThreshold", value = "1"),
            },
            groupKey = "ThreadService",
            commandKey = "thread",
            threadPoolKey = "ThreadService"
    )
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        //int i=1/0;
        System.out.println(Thread.currentThread().getName()+"天1"+"开始");
        try {
            Thread.sleep(500 * 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
      /*  new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println(Thread.currentThread().getName()+"天1");
                    try {
                        Thread.sleep(200 * 10);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println(Thread.currentThread().getName()+"天2");
                    try {
                        Thread.sleep(200 * 10);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println(Thread.currentThread().getName()+"天3");
                    try {
                        Thread.sleep(200 * 10);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();*/
        String result = paymentHystrixService.paymentInfo_OK(id);
        return result;
    }

    public String paymentInfo_TimeoutFallbackMethod(Integer id) {
        return "/(ToT)/我是消费者80，调用8001支付系统繁忙，请10秒钟后重新尝试、\t";
    }


}
